﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Navigation;
using CUWo.Common;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace CUWo.App
{
    public partial class IndexPage : PhoneApplicationPage
    {
        public IndexPage()
        {
            InitializeComponent();

            //
            this.AddHeaderToPivotItemInPivotMain();
            this.AddHeaderToPivotItemInPivotSetting();
            //                        
            //pivot.AddHandler(Pivot.ManipulationDeltaEvent, new EventHandler<System.Windows.Input.ManipulationDeltaEventArgs>((s, e) => { }), true);
            //pivot.AddHandler(Button.ManipulationDeltaEvent, new EventHandler<System.Windows.Input.ManipulationDeltaEventArgs>(Pivot_ManipulationDelta), true);
            //pivot.AddHandler(Button.ManipulationCompletedEvent, new EventHandler<System.Windows.Input.ManipulationCompletedEventArgs>(pivot_ManipulationCompleted), true);
        }

        /// <summary>
        /// 添加标题至PivotItem
        /// <para>因为代码中的多个PivotItem只能包含一个phone:PivotItem.Header控件</para>
        /// </summary>
        private void AddHeaderToPivotItemInPivotSetting()
        {
            double fontSize = 30;
            Color foreground = CUWo.Common.Helper.HexToColor("#ff000000");
            Thickness margin = new Thickness(0, -10, 0, 10);
            this.pivotSettingEmail.Header = new PivotItemHelper().CreateTextBlock("邮箱", fontSize, foreground, margin);
            this.pivotSettingMessage.Header = new PivotItemHelper().CreateTextBlock("消息", fontSize, foreground, margin);
            this.pivotSettingConfig.Header = new PivotItemHelper().CreateTextBlock("设置", fontSize, foreground, margin);
        }

        private void AddHeaderToPivotItemInPivotMain()
        {
            double fontSize = 50;
            Color foreground = CUWo.Common.Helper.HexToColor("#ff000000");
            Thickness margin = new Thickness(-10, -10, 0, 10);
            this.pivotMainTrack.Header = new PivotItemHelper().CreateTextBlock("流量", fontSize, foreground, margin);
            this.pivotMainContent.Header = new PivotItemHelper().CreateTextBlock("内容", fontSize, foreground, margin);
            this.pivotMainActive.Header = new PivotItemHelper().CreateTextBlock("推荐", fontSize, foreground, margin);
            this.pivotMainUserCenter.Header = new PivotItemHelper().CreateTextBlock("我的", fontSize, foreground, margin);
        }


        private void Pivot_ManipulationDelta(object sender, System.Windows.Input.ManipulationDeltaEventArgs e)
        {
            //e.Complete();
            //LogHelper.LogInfo(e.ManipulationOrigin.X.ToString(), this.GetType(), MethodBase.GetCurrentMethod());
        }

        private void pivot_ManipulationStarted(object sender, System.Windows.Input.ManipulationStartedEventArgs e)
        {
            double x = e.ManipulationOrigin.X;
            LogHelper.LogInfo(e.ManipulationOrigin.X.ToString(), this.GetType(), MethodBase.GetCurrentMethod());
            string headerTitle = ((this.pivotMain.SelectedItem as PivotItem).Header as TextBlock).Text;
            //if ("我的".Equals(headerTitle))
            {
                if (x >= 100 && x <= 380)
                {
                    //中间部分
                    this.pivotMain.IsLocked = true;
                    LogHelper.LogInfo("锁定：pivot.PivotItem！");
                    this.pivotSetting.IsLocked = false;
                    LogHelper.LogInfo("切换：pivotSetting.PivotItem");

                }
                else
                {
                    //外侧部分
                    this.pivotMain.IsLocked = false;
                    LogHelper.LogInfo("切换：pivot.PivotItem！");
                    this.pivotSetting.IsLocked = true;
                    LogHelper.LogInfo("锁定：pivotSetting.PivotItem！");
                }
            }
        }

        private void pivot_ManipulationCompleted(object sender, System.Windows.Input.ManipulationCompletedEventArgs e)
        {
            //LogHelper.LogInfo(e.ManipulationOrigin.X.ToString(), this.GetType(), MethodBase.GetCurrentMethod());
        }

        private void pivotSetting_ManipulationStarted(object sender, System.Windows.Input.ManipulationStartedEventArgs e)
        {
            double x = e.ManipulationOrigin.X;
            LogHelper.LogInfo(e.ManipulationOrigin.X.ToString(), this.GetType(), MethodBase.GetCurrentMethod());
            string headerTitle = ((this.pivotMain.SelectedItem as PivotItem).Header as TextBlock).Text;
            //if ("我的".Equals(headerTitle))
            {
                if (x < 100 || x > 380)
                {

                    this.pivotSetting.IsLocked = true;
                    LogHelper.LogInfo("锁定：pivotSetting.PivotItem！");
                    this.pivotMain.IsLocked = false;
                    LogHelper.LogInfo("切换：pivot.PivotItem！");

                }
                else
                {

                    this.pivotSetting.IsLocked = false;
                    LogHelper.LogInfo("切换：pivotSetting.PivotItem");
                    this.pivotMain.IsLocked = true;
                    LogHelper.LogInfo("锁定：pivot.PivotItem！");
                }
            }
        }
    }
}